﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
namespace GiaoDien
{
    public partial class FormTheDocGia : Form
    {
        private DataTable datatable;
        private string sqlquery;
        public FormTheDocGia()
        {
            InitializeComponent();
        }

        private void init()
        {
            datatable = new DataTable();
            sqlquery = "";
        }
        private void Loadngaysinh()
        {
            for (int i = 1; i <= 31; i++)
                cbbngaysinh.Items.Add(i);
            cbbngaysinh.SelectedIndex = 0;
        }
        private void Loadthangsinh()
        {
            for (int i = 1; i <= 12; i++)
                cbbthangsinh.Items.Add(i);
            cbbthangsinh.SelectedIndex = 0;
        }
        private void Loadnamsinh()
        {
            int yearstart = DateTime.Now.Year - 60;
            for (int i = yearstart; i <= DateTime.Now.Year; i++)
                cbbnamsinh.Items.Add(i);
            cbbnamsinh.SelectedIndex = 0;
        }

        private void Loadloaidocgia()
        {
            LOAIDOCGIABUS ldgBUS = new LOAIDOCGIABUS();
            DataTable data = ldgBUS.Loadloaidocgia();
            cbbloaidocgia.DataSource = data;
            cbbloaidocgia.DisplayMember = "TenLoaiDocGia";
            cbbloaidocgia.ValueMember = "MaLoaiDocGia";
            cbbloaidocgia.SelectedIndex = 0;
        }

        private void Loadngaylapthe()
        {
            tbngaylapthe.Text = DateTime.Now.ToString();
        }
        private void xemtatca()
        {
            DOCGIABUS dgBUS = new DOCGIABUS();
            datatable = dgBUS.Loaddocgia();
            this.dataGridView1.DataSource = datatable;
        }

        private void FormTheDocGia_Load(object sender, EventArgs e)
        {
            init();
            xemtatca();
            Loadngaysinh();
            Loadthangsinh();
            Loadnamsinh();
            Loadloaidocgia();
            Loadngaylapthe();
            Madocgia();
        }

        private void btnxemtatca_Click(object sender, EventArgs e)
        {
            xemtatca();
        }

        private void btnlapthe_Click(object sender, EventArgs e)
        {
            DOCGIADTO dgDTO = new DOCGIADTO();
            dgDTO.Madocgia = tbmadocgia.Text.Trim();
            dgDTO.Hoten = tbhoten.Text.Trim().ToUpper();

            int day = Convert.ToInt32(cbbngaysinh.SelectedItem.ToString());
            int month = Convert.ToInt32(cbbthangsinh.SelectedItem.ToString());
            int year = Convert.ToInt32(cbbnamsinh.SelectedItem.ToString());
            dgDTO.Ngaysinh = new DateTime(year, month, day);

            dgDTO.Diachi = tbdiachi.Text.Trim().ToUpper();
            dgDTO.Email = tbemail.Text.Trim().ToUpper();
            dgDTO.Dienthoai = tbdienthoai.Text.Trim().ToUpper();
            dgDTO.Maloaidocgia = cbbloaidocgia.SelectedValue.ToString();

            dgDTO.Ngaylapthe = Convert.ToDateTime(tbngaylapthe.Text);
            dgDTO.Ngayhethan = dgDTO.Ngaylapthe.AddDays(365);
            dgDTO.Tienno = 0;
            dgDTO.Matinhtrang = "HĐ";
            dgDTO.Manhanvien = ManHinhChinh.manhanvien;

            DOCGIABUS dgBUS = new DOCGIABUS();
            int kq = dgBUS.themdocgia(dgDTO);
            if (kq > 0)
            {
                MessageBox.Show("Thêm thành công");
                xemtatca();
            }
            else MessageBox.Show("Thêm thất bại");
        }

        private void btnthemoi_Click(object sender, EventArgs e)
        {
            tbmadocgia.ResetText();
            tbhoten.ResetText();
            cbbngaysinh.SelectedIndex = 0;
            cbbthangsinh.SelectedIndex = 0;
            cbbnamsinh.SelectedIndex = 0;
            tbdiachi.ResetText();
            tbemail.ResetText();
            tbdienthoai.ResetText();
            cbbloaidocgia.SelectedIndex = 0;
            tbngaylapthe.ResetText();
            Madocgia();
        }

        private void capnhat()
        {
            sqlquery = "SELECT * FROM DOC_GIA";
            DOCGIABUS dgBUS = new DOCGIABUS();
            int kq = dgBUS.update(sqlquery, datatable);
            if (kq > 0)
                MessageBox.Show("Thành công");
            else
                MessageBox.Show("Thất bại");
        }

        private void btncapnhat_Click(object sender, EventArgs e)
        {
            capnhat();
            
        }

        private void tracuuma()
        {
            DataView dataview = new DataView(datatable);
            string filter = "MaDocGia = '" + tbmadocgia2.Text.Trim().ToUpper() + "'";
            dataview.RowFilter = filter;
            this.dataGridView1.DataSource = dataview;
        }

        private void tracuuten()
        {
            DataView dataview = new DataView(datatable);
            string filter = "HoTen = '" + tbhoten2.Text.Trim().ToUpper() + "'";
            dataview.RowFilter = filter;
            this.dataGridView1.DataSource = dataview;
        }

        private void btntracuu_Click(object sender, EventArgs e)
        {
            if (tbmadocgia.Text != "")
                tracuuma();
            else if (tbhoten2.Text != "")
                tracuuten();
        }

        private void bnthoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Madocgia()
        {
            DOCGIABUS dgBUS = new DOCGIABUS();
            string maloai = cbbloaidocgia.SelectedValue.ToString().Trim();
            if (maloai != "")
            {
                tbmadocgia.Text = "";
                tbmadocgia.Text = dgBUS.madocgia(maloai);
            }
        }

        private void cbbloaidocgia_SelectedIndexChanged(object sender, EventArgs e)
        {
            Madocgia();
        }
    }
}
